﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace ZZQFrameWork.AI
{
    public class EntityParent:MonoBehaviour
    {
        /// <summary>
        /// 黑板
        /// </summary>
        public BlackBoard blackBoard=new BlackBoard();

        /// <summary>
        /// 行为树根节点
        /// </summary>
        public BehaviorTreeRoot root;

        /// <summary>
        /// 当前所在节点
        /// </summary>
        public Node nowNode;

        /// <summary>
        /// 当前选择节点
        /// </summary>
        private Node selectNode;

        public Node SelectNode
        {
            get { return selectNode; }
            set
            {
                selectNode = value;
                if (selectNode == null)
                {
                    blackBoard.AIState = AIState.IDLE_STATE;
                    blackBoard.Enemy = null;
                }
            }
        }

        /// <summary>
        /// 当前寻路路径
        /// </summary>
        public List<Node> findPathList=new List<Node>();

        public float lastThinkTime = 0;

        void Awake()
        {
            lastThinkTime = Time.realtimeSinceStartup;//获取当前时间

            OnAwake();
        }

        void Start()
        {
            OnStart();
        }

        void Update()
        {
            DummyThink();
            OnUpdate();
        }

        private void DummyThink()
        {
            if (Time.realtimeSinceStartup - lastThinkTime > 1)
            {
                Think();
            }
        }

        protected virtual int CanThink()
        {
            return 0;
        }

        public virtual void BeforeThink() { }

        public virtual void Think()
        {
            lastThinkTime = Time.realtimeSinceStartup;
            if (CanThink() != 0) return;//有错误，不执行

            BeforeThink();

            root.Proc(this);

            AfterThink();
        }

        public virtual void AfterThink() { }

        #region 虚方法

        protected virtual void OnAwake()
        {

        }

        protected virtual void OnStart()
        {

        }

        protected virtual void OnUpdate()
        {

        }

        #endregion

        #region Proc

        public virtual Enum_BTState ProcIdle()
        {
            return Enum_BTState.Success;
        }

        public virtual Enum_BTState ProcMoveTo()
        {
            return Enum_BTState.Success;
        }

        public virtual Enum_BTState ProcAttackTo()
        {
            return Enum_BTState.Success;
        }
        public Enum_BTState ProcFreedSkill()
        {
            return Enum_BTState.Success;
        }

        public virtual SkillVO ProcIsDoSkill()
        {
            return null;
        }

        public virtual SkillVO ProcHasNextSkill()
        {
            return null;
        }

        public virtual bool ProcIsNextSkillInColdTime()
        {
            return true;
        }

        #endregion

        /// <summary>
        /// 被攻击
        /// </summary>
        /// <param name="attacker">攻击者</param>
        /// <param name="atk">攻击力</param>
        public virtual void BeHit(EntityParent attacker,int atk)
        {
            
        }

    }
}
